from django.shortcuts import render_to_response
from django.conf import settings
from django.http import HttpResponseRedirect, Http404
from django.core.paginator import Paginator
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django import forms
from django.conf import settings
from django.utils.simplejson import *
import blogs.captcha
from blogs.models import Blog, Post, Tag, Comment, Banned, Tag
from blogs.forms import CommentForm
from blogs.utils import comment_utils, get_cached_object, get_response_dict
    
def index(request, blog_slug, page_num=1):
    
    blog = get_cached_object('Blog:%s' % blog_slug, Blog, pk=blog_slug)
    
    page_num = int(page_num)
    
    cache_key = 'Blog:%s_posts' % blog_slug
    
    posts = cache.get(cache_key)
    
    if posts == None:
        posts = blog.get_posts()
        cache.set(cache_key, list(posts), settings.DEFAULT_CACHE_TIME)
        
    paginator = Paginator(posts, blog.at_most)
    
    page = paginator.page(page_num)
    
    return render_to_response('blogs/posts.html', get_response_dict(blog, request.user, {
        'page_class': 'index',
        'posts': page.object_list,
        'page_num': page_num,
        'has_previous_page': page.has_previous(),
        'has_next_page': page.has_next(),
        'older_url': reverse('blogs.main.page', args=[blog.slug, page_num+1]),
        'newer_url': reverse('blogs.main.page', args=[blog.slug, page_num-1]),
    }))


def page(request, blog_slug, page_num):
    return index(request, blog_slug, page_num=int(page_num))



def post(request, blog_slug, year, month, day, post_slug):
    
    blog = get_cached_object('Blog:%s' % blog_slug, Blog, pk=blog_slug)
    post = get_cached_object('Post:%s' % post_slug, Post, blog=blog_slug, slug=post_slug)
    
    (new_comment_id, comments, comment_form, allowed, user_ip, captcha_error) = \
        comment_utils(request, blog, post)
    
    return render_to_response('blogs/post.html', get_response_dict(blog, request.user, {
        'page_class': 'post',
        'post': post,
        'comments': comments,
        'comment_form': comment_form,
        'allowed': allowed,
        'user_ip':user_ip,
        'recaptcha_key': settings.RECAPTCHA_PUB_KEY,
        'captcha_error': captcha_error,
        'RECAPTCHA_SETTINGS': JSONEncoder().encode(settings.RECAPTCHA_SETTINGS),
    }))
    
    

def tag(request, blog_slug, tag_name, page_num=1):
    
    blog = get_cached_object('Blog:%s' % blog_slug, Blog, pk=blog_slug)
    
    cache_key = 'Blog:%s_Tag:%s' % (blog_slug, tag_name)
    
    posts = cache.get(cache_key)
    
    if posts == None:
        posts = Post.objects.filter(blog=blog, pub_date__isnull=False, \
            tags__name__exact=tag_name).order_by('-pub_date')
        cache.set(cache_key, list(posts), settings.DEFAULT_CACHE_TIME)
    
    if not posts:
        raise Http404
    
    page_num = int(page_num)
    
    paginator = Paginator(posts, blog.at_most)   
    
    page = paginator.page(page_num) 

    return render_to_response('blogs/tag.html', get_response_dict(blog, request.user, {
        'page_class': 'tag',
        'tag_name': tag_name,
        'posts': page.object_list,
        'has_previous_page': page.has_previous(),
        'has_next_page': page.has_next(),
        'older_url': reverse('blogs.main.tag', args=[blog.slug, tag_name, page_num+1]),
        'newer_url':reverse('blogs.main.tag', args=[blog.slug, tag_name, page_num-1]),
    }))
    
    

def tags(request, blog_slug):
    
    blog = get_cached_object('Blog:%s' % blog_slug, Blog, pk=blog_slug) 

    return render_to_response('blogs/tags.html', get_response_dict(blog, request.user, {}))


def archives_day(request, blog_slug, year, month, day, page_num=1):

    blog = get_cached_object('Blog:%s' % blog_slug, Blog, pk=blog_slug)
    
    cache_key = 'Blog:%s_archive:%s_%s_%s' % (blog_slug, year, month, day)
    posts = cache.get(cache_key)
    
    if posts == None:
        posts =  Post.objects.filter(blog=blog, pub_date__isnull=False, \
            pub_date__year=year, pub_date__month=month, pub_date__day=day).order_by('-pub_date')
        cache.set(cache_key, list(posts), settings.DEFAULT_CACHE_TIME)
    
    page_num = int(page_num)
    paginator = Paginator(posts, blog.at_most) 
    page = paginator.page(page_num)

    return render_to_response('blogs/archives_day.html', get_response_dict(blog, request.user, {
        'page_class': 'archives',
        'year': year,
        'month': month,
        'day': day,
        'posts': page.object_list,
        'has_previous_page': page.has_previous(),
        'has_next_page': page.has_next(),
        'older_url': reverse('blogs.main.archives_day_page', args=[blog.slug, year, month, day, page_num+1]),
        'newer_url': reverse('blogs.main.archives_day_page', args=[blog.slug, year, month, day, page_num-1]),
    }))


def archives_day_page(request, blog_slug, year, month, day, page_num):
    return archives_day(request, blog_slug, year, month, day, page_num)


def archives_month(request, blog_slug, year, month, page_num=1):

    blog = get_cached_object('Blog:%s' % blog_slug, Blog, pk=blog_slug)
    
    cache_key = 'Blog:%s_archive:%s_%s' % (blog_slug, year, month)
    posts = cache.get(cache_key)
    
    if posts == None:
        posts =  Post.objects.filter(blog=blog, pub_date__isnull=False, \
            pub_date__year=year, pub_date__month=month).order_by('-pub_date')
        cache.set(cache_key, list(posts), settings.DEFAULT_CACHE_TIME)
      
    page_num = int(page_num)
    paginator = Paginator(posts, blog.at_most) 
    page = paginator.page(page_num)

    return render_to_response('blogs/archives_month.html', get_response_dict(blog, request.user, {
        'page_class': 'archives',
        'year': year,
        'month': month,
        'posts': page.object_list,
        'has_previous_page': page.has_previous(),
        'has_next_page': page.has_next(),
        'older_url': reverse('blogs.main.archives_month_page', args=[blog.slug, year, month, page_num+1]),
        'newer_url':reverse('blogs.main.archives_month_page', args=[blog.slug, year, month, page_num-1]),
    }))


def archives_month_page(request, blog_slug, year, month, page_num):
    return archives_month(request, blog_slug, year, month, page_num)


def archives_year(request, blog_slug, year, page_num=1):

    blog = get_cached_object('Blog:%s' % blog_slug, Blog, pk=blog_slug)
    
    cache_key = 'Blog:%s_archive:%s' % (blog_slug, year)
    posts = cache.get(cache_key)
    
    if posts == None:
        posts =  Post.objects.filter(blog=blog, pub_date__isnull=False, pub_date__year=year).order_by('-pub_date')
        cache.set(cache_key, list(posts), settings.DEFAULT_CACHE_TIME)
        
    page_num = int(page_num)
    paginator = Paginator(posts, blog.at_most) 
    page = paginator.page(page_num)

    return render_to_response('blogs/archives_year.html', get_response_dict(blog, request.user, {
        'page_class': 'archives',
        'year': year,
        'posts': page.object_list,
        'has_previous_page': page.has_previous(),
        'has_next_page': page.has_next(),
        'older_url': reverse('blogs.main.archives_year_page', args=[blog.slug, year, page_num+1]),
        'newer_url':reverse('blogs.main.archives_year_page', args=[blog.slug, year, page_num-1]),
    }))


def archives_year_page(request, blog_slug, year, page_num):
    return archives_year(request, blog_slug, year, page_num)


def search(request, blog_slug):

    blog = get_cached_object('Blog:%s' % blog_slug, Blog, pk=blog_slug)
    
    query = request.GET.get('query')
    page_num = request.GET.get('page')
    
    if page_num == None:
        page_num = 1
    else:
        page_num = int(page_num)

    posts =  Post.objects.filter(blog=blog_slug, pub_date__isnull=False, body__search=query)
    
    num_results = 10
    
    paginator = Paginator(posts, num_results)
    
    page = paginator.page(page_num)
    
    results = page.object_list

    return render_to_response('blogs/search_results.html', get_response_dict(blog, request.user, {
        'page_class': 'search',
        'query': query,
        'start': (page_num-1) * num_results + 1,
        'end': (page_num-1) * num_results + len(results),
        'total': posts.count(),
        'posts': results,
        'has_previous_page': page.has_previous(),
        'has_next_page': page.has_next(),
        'prev_page': page_num-1,
        'next_page': page_num+1,
    }))
